package com.qianfan.controller;

import org.springframework.ai.ResourceUtils;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.qianfan.QianFanChatModel;
import org.springframework.ai.qianfan.QianFanChatOptions;
import org.springframework.ai.qianfan.api.QianFanApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.stringtemplate.v4.ST;

import java.util.List;

/**
 * @program: springboot3-demos
 * @description:
 * @author: Reagan Li
 * @create: 2024-07-04 10:02
 **/

@RestController
@RequestMapping("/api")
public class ChatController {

    //QianFanApi qianFanApi = new QianFanApi(System.getenv("QIANFAN_API_KEY"), System.getenv("QIANFAN_SECRET_KEY"));

    @Autowired
    private QianFanChatModel chatModel;

    @GetMapping("chat")
    public ResponseEntity chat(@RequestParam("msg") String msg) {
        /*QianFanApi.ChatCompletionMessage chatCompletionMessage = new QianFanApi.ChatCompletionMessage("Hello world", QianFanApi.ChatCompletionMessage.Role.USER);
        ResponseEntity<QianFanApi.ChatCompletion> response = qianFanApi.chatCompletionEntity(new QianFanApi.ChatCompletionRequest(
                List.of(chatCompletionMessage), buildSystemMessage(), "ernie_speed", 0.7f, false));*/

        //String response = chatModel.call(msg);

        ChatResponse response = chatModel.call(
                new Prompt(
                        "Generate the names of 5 famous pirates.",
                        QianFanChatOptions.builder()
                                .withModel(QianFanApi.ChatModel.ERNIE_Speed_8K.getValue())
                                .withTemperature(0.5f)
                                .build()
                ));
        return ResponseEntity.ok(response);
    }

    /*private String buildSystemMessage() {
        String systemMessageTemplate = ResourceUtils.getText("classpath:/prompts/system-message.st");
        ST st = new ST(systemMessageTemplate, '{', '}');
        return st.add("name", "QianFan").add("voice", "pirate").render();
    }*/
}
